﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VipSys.SystemEntity;
using System.Data;
using VipSys.Common.Library;
using System.Diagnostics;

namespace VipSys.DataLayer
{
    public class StationAccess
    {
        private static StationAccess m_Instance = null;
        /// <summary>
        /// 单实例
        /// </summary>
        public static StationAccess Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new StationAccess();
                return m_Instance;
            }
        }

        public short Add(Station station)
        {
            string szFiled = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}"
                , SystemData.StationTable.DepartmentID
                , SystemData.StationTable.EmployeeID
                , SystemData.StationTable.H
                , SystemData.StationTable.OfficeID
                , SystemData.StationTable.PicStationID
                , SystemData.StationTable.Status
                , SystemData.StationTable.W
                , SystemData.StationTable.X
                , SystemData.StationTable.Y);
            string szValue = string.Format("'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}'"
                , station.DepartmentID
                , station.EmployeeID
                , station.H
                , station.OfficeID
                , station.PicStationID
                , station.Status
                , station.W
                , station.X
                , station.Y);
            string szSql = string.Format("Insert Into {0}({1}) values({2})"
                , SystemData.DBTable.Station
                , szFiled
                , szValue);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short Edit(Station station)
        {
            string szFiledValue = string.Format("{0}='{1}',{2}='{3}',{4}='{5}',{6}='{7}',{8}='{9}',{10}='{11}',{12}='{13}',{14}='{15}',{16}='{17}'"
                , SystemData.StationTable.DepartmentID, station.DepartmentID
                , SystemData.StationTable.EmployeeID , station.EmployeeID
                , SystemData.StationTable.H , station.H
                , SystemData.StationTable.OfficeID , station.OfficeID
                , SystemData.StationTable.PicStationID, station.PicStationID
                , SystemData.StationTable.Status ,station.Status
                , SystemData.StationTable.W, station.W
                , SystemData.StationTable.X ,station.X
                , SystemData.StationTable.Y ,station.Y);
            string szCondition = string.Format(" {0}={1}"
                , SystemData.StationTable.ID, station.ID);
            string szSql = string.Format("update {0} set {1} where {2}"
                , SystemData.DBTable.Station
                , szFiledValue
                , szCondition);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
               
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short Del(Station station)
        {
            string szCondition = string.Format(" {0}={1}"
                , SystemData.StationTable.ID, station.ID);
            string szSql = string.Format("Delete from {0}  where {1}"
                , SystemData.DBTable.Station
                , szCondition);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }


        public short GetStationList(Station query, ref List<Station> lstStation)
        {
            if (lstStation == null)
                lstStation = new List<Station>();
            lstStation.Clear();
            string szField = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}"
                , SystemData.StationTable.DepartmentID
                , SystemData.StationTable.EmployeeID
                , SystemData.StationTable.H
                , SystemData.StationTable.ID
                , SystemData.StationTable.OfficeID
                , SystemData.StationTable.PicStationID
                , SystemData.StationTable.W
                , SystemData.StationTable.X
                , SystemData.StationTable.Y
                , SystemData.StationTable.Status);
            string szCondition=string.Format("1=1");
            if (!string.IsNullOrEmpty(query.OfficeID))
                szCondition = string.Format("{0} and {1}='{2}'"
                    , szCondition
                    , SystemData.StationTable.OfficeID, query.OfficeID);
            string szSql=string.Format("select {0} from {1} where {2}"
                , szField
                , SystemData.DBTable.Station
                , szCondition);

            try
            {
                DataSet ds=SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Station station = new Station();
                    station.ID =int.Parse(ds.Tables[0].Rows[i][SystemData.StationTable.ID].ToString());
                    station.DepartmentID = ds.Tables[0].Rows[i][SystemData.StationTable.DepartmentID].ToString();
                    station.EmployeeID = ds.Tables[0].Rows[i][SystemData.StationTable.EmployeeID].ToString();
                    station.H = ds.Tables[0].Rows[i][SystemData.StationTable.H].ToString();
                    station.OfficeID = ds.Tables[0].Rows[i][SystemData.StationTable.OfficeID].ToString();
                    station.PicStationID = ds.Tables[0].Rows[i][SystemData.StationTable.PicStationID].ToString();
                    station.Status = ds.Tables[0].Rows[i][SystemData.StationTable.Status].ToString();
                    station.W = ds.Tables[0].Rows[i][SystemData.StationTable.W].ToString();
                    station.X = ds.Tables[0].Rows[i][SystemData.StationTable.X].ToString();
                    station.Y = ds.Tables[0].Rows[i][SystemData.StationTable.Y].ToString();
                    lstStation.Add(station);
                }
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
        public short GetStation(Station query, ref Station station)
        {
            string szField = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}"
                 , SystemData.StationTable.DepartmentID
                 , SystemData.StationTable.EmployeeID
                 , SystemData.StationTable.H
                 , SystemData.StationTable.ID
                 , SystemData.StationTable.OfficeID
                 , SystemData.StationTable.PicStationID
                 , SystemData.StationTable.W
                 , SystemData.StationTable.X
                 , SystemData.StationTable.Y);

            string szCondition = string.Format("1=1");
            if (!string.IsNullOrEmpty(query.OfficeID))
                szCondition = string.Format("{0} and {1}='{2}'"
                    , szCondition
                    , SystemData.StationTable.OfficeID, query.OfficeID);
            if(query.ID!=0)
                szCondition = string.Format("{0} and {1}={2}"
                  , szCondition
                  , SystemData.StationTable.ID, query.ID.ToString());
           
            string szSql = string.Format("select {0} from {1} where {2}"
                , szField
                , SystemData.DBTable.Station
                , szCondition);
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                    return SystemData.ReturnValue.RES_NO_FOUND;
                station.ID = int.Parse(ds.Tables[0].Rows[0][SystemData.StationTable.ID].ToString());
                station.DepartmentID = ds.Tables[0].Rows[0][SystemData.StationTable.DepartmentID].ToString();
                station.EmployeeID = ds.Tables[0].Rows[0][SystemData.StationTable.EmployeeID].ToString();
                station.H = ds.Tables[0].Rows[0][SystemData.StationTable.H].ToString();
                station.OfficeID = ds.Tables[0].Rows[0][SystemData.StationTable.OfficeID].ToString();
                station.PicStationID = ds.Tables[0].Rows[0][SystemData.StationTable.PicStationID].ToString();
                station.Status = ds.Tables[0].Rows[0][SystemData.StationTable.Status].ToString();
                station.W = ds.Tables[0].Rows[0][SystemData.StationTable.W].ToString();
                station.X = ds.Tables[0].Rows[0][SystemData.StationTable.X].ToString();
                station.Y = ds.Tables[0].Rows[0][SystemData.StationTable.Y].ToString();
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

    }
}
